package ms.platform.system.controller;

import ms.platform.system.model.Admin;
import ms.platform.system.model.WorkingProcedureTemplate;
import ms.platform.system.service.WorkingProcedureTemplateService;
import ms.platform.util.PageUtils;
import ms.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/workingProcedureTemplate/")
public class WorkingProcedureTemplateController extends BaseController {

    @Autowired
    private WorkingProcedureTemplateService workingProcedureTemplateService;

    /**
     * 获取模板
     *
     * @return
     */
    @RequestMapping(value = "/template/list")
    public String getAllWorkingProcedureTemplate(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        map.put("count", workingProcedureTemplateService.queryTemplateCount(map));
        List<WorkingProcedureTemplate> manufacturerAllList = workingProcedureTemplateService.getAllWorkingProcedureTemplate(PageUtils.Page(request, map));
        request.setAttribute("templateList", manufacturerAllList);
        PageUtils.calculate(map);
        request.setAttribute("searchHistory", map);
        return "system/workingProcedure/workingProcedureTemplate";
    }

    @RequestMapping(value = "/templateById")
    public String getTemplate(String code, HttpServletRequest request) {
        if (code != null) {
            List<WorkingProcedureTemplate> templates = workingProcedureTemplateService.queryPageInfo(code);
            request.setAttribute("templates", templates);
            request.setAttribute("templatesLevel", templates.get(0).getLevel());
            request.setAttribute("templatesType", templates.get(0).getType());

            request.setAttribute("templatesCode", code);
            return "system/workingProcedure/edit";
        }
        return "system/workingProcedure/addTemplate";
    }

    //批量添加模板
    @RequestMapping(value = "/updateOrAdd/template")
    public Object insertTemplate(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("adminUser");//当前操作的人
        String user = admin.getUsername();
        //工序号
        Integer count = workingProcedureTemplateService.checkCount();//每次加1
        List<WorkingProcedureTemplate> templates = new ArrayList<>();
        List<String> infos = new ArrayList<>();// 对应数,工序名,工序精度,参考耗时,耗时,耗时单位,难易度,类型

        for (Map.Entry<String, Object> info : map.entrySet()) {
            infos.add((String) info.getValue());
        }
        WorkingProcedureTemplate template = null;
        for (int i = 0; i < infos.size(); i += 8) {
            template = new WorkingProcedureTemplate();
            if (i >= infos.size()) {
                break;
            }
            // 对应数,工序名,工序精度,耗时,耗时单位,难易度,类型
            template.setSortNum(Integer.valueOf(infos.get(i)));//对应数
            template.setName(infos.get(i + 1));//工序名
            template.setPrecisionNum(infos.get(i + 2));//工序精度
            template.setReferenceDate(Float.valueOf(infos.get(i + 3)));//参考耗时
            template.setDefTime(Float.valueOf(infos.get(i + 4)));//耗时
            template.setDefTimeUnit(infos.get(i + 5));//耗时单位
            template.setLevel(infos.get(i + 6));//难易度
            template.setType(infos.get(i + 7));//类型
            template.setCode(count.toString());
            template.setInsertedBy(user);//操作人
            templates.add(template);
        }
        //查询 难度 类型,如果存在,则删除code
        List<WorkingProcedureTemplate> templ = workingProcedureTemplateService.queryCount(template);
        if (templ.size() > 0) {
            workingProcedureTemplateService.deleteCodeTemplates(templ.get(0).getCode());
        }
        workingProcedureTemplateService.insertBatch(templates);
        return returnSuccess("操作成功", templates);
    }

    //修改保存
    @RequestMapping("/save/templates")
    @ResponseBody
    public Object saveTemplates(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("adminUser");//当前操作的人
        String user = admin.getUsername();
        List<WorkingProcedureTemplate> templates = new ArrayList<>();
        List<WorkingProcedureTemplate> addTemplates = new ArrayList<>();
        List<String> infos = new ArrayList<>();// 对应数,工序名,工序精度,耗时,耗时单位,难易度,类型

        for (Map.Entry<String, Object> info : map.entrySet()) {
            infos.add((String) info.getValue());
        }
        int index = 1;
        for (int i = 0; i < infos.size(); i += 10) {
            WorkingProcedureTemplate template = new WorkingProcedureTemplate();
            if (i >= infos.size()) {
                break;
            }
            //id 版本号 对应数,工序名,工序精度,参考耗时,耗时,耗时单位,难易度,类型
            template.setCode(infos.get(i + 1));//版本号
            template.setSortNum(index);//对应数
            template.setName(infos.get(i + 3));//工序名
            template.setPrecisionNum(infos.get(i + 4));//工序精度
            template.setReferenceDate(Float.valueOf(infos.get(i + 5)));//参考耗时
            template.setDefTime(Float.valueOf(infos.get(i + 6)));//耗时
            template.setDefTimeUnit(infos.get(i + 7));//耗时单位
            template.setLevel(infos.get(i + 8));//难易度
            template.setType(infos.get(i + 9));//类型

            if (StringUtil.isNullOrBlock(infos.get(i))) {
                //如果为空 为添加
                template.setInsertedBy(user);//操作人
                addTemplates.add(template);
            } else {
                template.setUpdatedBy(user);//操作人
                template.setId(Integer.valueOf(infos.get(i)));//版本号
                templates.add(template);
            }
            index++;
        }
        workingProcedureTemplateService.saveTemplates(templates);
        if (addTemplates.size() > 0) {
            workingProcedureTemplateService.insertBatch(addTemplates);//批量添加
        }
        return returnSuccess("操作成功", templates);
    }

    //删除一个模板
    @RequestMapping(value = "/delete/templates")
    public Object deleteTemplates(Integer id, HttpServletRequest request) {
        workingProcedureTemplateService.deleteTemplates(id);
        return returnSuccess("操作成功", id);
    }

    //删除序号下的所有模板
    @RequestMapping(value = "/deleteCode/templates")
    public Object deleteCodeTemplates(String code, HttpServletRequest request) {
        workingProcedureTemplateService.deleteCodeTemplates(code);
        return returnSuccess("操作成功", code);
    }

    //模板临时保存数据
    @RequestMapping(value = "/temp/template")
    public @ResponseBody
    Map<String, Object> tempAjaxTemplate(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        Map<String, Object> mapSystem = new HashMap<String, Object>();

        List<WorkingProcedureTemplate> templates = new ArrayList<>();
        List<String> infos = new ArrayList<>();

        for (Map.Entry<String, Object> info : map.entrySet()) {
            infos.add((String) info.getValue());
        }
        for (int i = 0; i < infos.size(); i += 8) {
            WorkingProcedureTemplate template = new WorkingProcedureTemplate();
            if (i >= infos.size()) {
                break;
            }
            // 对应数,工序名,工序精度,参考耗时,耗时,耗时单位,难易度,类型
            template.setSortNum(Integer.valueOf(infos.get(i)));//对应数
            template.setName(infos.get(i + 1));//工序名
            template.setPrecisionNum(infos.get(i + 2));//工序精度
            template.setReferenceDate(Float.valueOf(infos.get(i + 3)));//参考耗时
            if (!StringUtil.isNullOrBlock(infos.get(i + 4))) {
                template.setDefTime(Float.valueOf(infos.get(i + 4)));//耗时
            }
            template.setDefTimeUnit(infos.get(i + 5));//耗时单位
            template.setLevel(infos.get(i + 6));//难易度
            template.setType(infos.get(i + 7));//类型
            templates.add(template);
        }
        mapSystem.put("tempTemplate", templates);
        return mapSystem;
    }

}
